# -*-coding:utf-8-*-
'''
File Create by Author:Backpackerxl
Create File Time:2021/9/27/027 18:57
Program IDE:PyCharm
File Name:TestMogoDB.py

'''
import time as t

from pymongo import MongoClient


class Test:
    def __init__(self):
        self.client = MongoClient('1.14.60.30', 27017)
        # print(self.client)

    def getDBs(self):
        dbs = self.client.list_database_names()
        for db in dbs:
            print(db)

    def getColl(self):
        mogondb = self.client['mongo']
        collections = mogondb.list_collection_names()
        for collection in collections:
            print(collection)

    def createColl(self):
        mogondb = self.client['mongo']
        mogondb.create_collection("student")

    def dropColl(self):
        mogondb = self.client['mongo']
        mogondb.drop_collection("student")

    def findDoc(self):
        self.mogondb = self.client["mongo"]
        conction = self.mogondb["myCollection"]
        documents = conction.find()
        for document in documents:
            print(document)

    def insertOneDoc(self):
        time_stamp = t.localtime(t.time())
        createTime = lambda \
                time_stamp: f'{time_stamp[0]}年{time_stamp[1]}月{time_stamp[2]}日 {time_stamp[3]}:{time_stamp[4]}:{time_stamp[5]}'
        self.mogondb = self.client["mongo"]
        comment = self.mogondb["myCollection"]
        newDoc = {'articleid': '1004', 'content': 'hahahh', 'userid': '004', 'nickname': '王立', 'age': 30.0,
                  'phone': '18783520431', 'createTiem': 'Date()', 'likenum': f'{createTime(time_stamp)}', 'state': 'ok'}
        comment.insert_one(newDoc)


if __name__ == "__main__":
    test = Test()
    # test.getDBs()
    # test.getColl()
    # test.createColl()
    # test.dropColl()
    test.findDoc()
    # test.insertOneDoc()
